package com.agino.jdk8.learn.threadLock.synchronizedlearn;

import com.agino.jdk8.learn.threadLock.synchronizedlearn.entity.User;
import com.agino.jdk8.learn.utils.PrintUtils;

/**
 * @author agino
 * @version 1.0
 * @date 2023/2/27 11:09
 */
public class SynchronizedClass {

    public synchronized void setAgeMethod() throws InterruptedException {
        User user = CommonResource.CURRENT_USER;
        PrintUtils.printLog("开始 user：" + user);
        Thread.sleep(5000);
        user.setAge(12);
        PrintUtils.printLog("结束 user：" + user);
    }

    public void setAgeCodeBlock() throws InterruptedException {
        User user = CommonResource.CURRENT_USER;
        PrintUtils.printLog("开始 user：" + user);
        synchronized (this) {
            Thread.sleep(5000);
            user.setAge(12);
        }
        PrintUtils.printLog("结束 user：" + user);
    }

    public synchronized void setNameMethod() throws InterruptedException {
        User user = CommonResource.CURRENT_USER;
        PrintUtils.printLog("开始 user：" + user);
        Thread.sleep(5000);
        user.setName("张三");
        PrintUtils.printLog("结束 user：" + user);
    }
}
